package com.cf.auth.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.map.MapUtil;
import com.cf.common.constant.RedisConstant;
import com.cf.common.result.Result;
import com.wf.captcha.SpecCaptcha;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;

/**
 * @author ChenFeng
 * @date 2021/11/5 2:15 下午
 * @description
 */
@RestController
@RequestMapping("/oauth")
@AllArgsConstructor
public class AuthController {

    private final RedisTemplate redisTemplate;

    @GetMapping("/captcha")
    public Result captcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String verCode = specCaptcha.text().toLowerCase();
        System.out.println("验证码：" + verCode);
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为30分钟
        redisTemplate.opsForValue().set(RedisConstant.CAPTCHA_KEY + key, verCode, Duration.ofMinutes(30));
        // 将key和base64返回给前端
        return Result.success(MapUtil.builder()
                .put("key", key)
                .put("image", specCaptcha.toBase64())
                .build()
        );
    }

}
